/*
@title: Bricks And Boxes
@description: Push the boxes out of your way and get to the flags to win!
@author: Amino47
@tags: []
@addedOn: 2024-11-11
*/

const player = "p"
const flag = "f"
const block = "b"
const pushable = "d"
const bg = "g"
const song = tune`
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + E4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + B4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: D4~389.61038961038963 + G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + C5~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: E4~389.61038961038963 + G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + B4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: D4~389.61038961038963 + G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + C5~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + E4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + B4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + D4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + C5~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + E4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + B4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + D4~389.61038961038963,
389.61038961038963: G4~389.61038961038963,
389.61038961038963: G4~389.61038961038963 + C5~389.61038961038963`
const win = tune`
216.60649819494586,
108.30324909747293: A4~108.30324909747293,
108.30324909747293: B4~108.30324909747293,
108.30324909747293: C5~108.30324909747293,
108.30324909747293: D5~108.30324909747293,
108.30324909747293: E5~108.30324909747293,
108.30324909747293: F5~108.30324909747293,
108.30324909747293: G5~108.30324909747293,
108.30324909747293: A5~108.30324909747293,
108.30324909747293: B5~108.30324909747293,
2274.3682310469317`
setLegend(
  [player, bitmap`
................
................
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HH5HHHHHH5HH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HH77777777HH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
..HHHHHHHHHHHH..
................
................`],
  [flag, bitmap`
................
................
............444.
.......44444444.
.......44444444.
.......44444444.
.......444444444
.......444444444
.......444444444
.......44444444.
.......4........
.......4........
.......4........
.......4........
.......4........
................`],
  [block, bitmap `
CCCCCCCCCC1CCCCC
CCCCCCCCCC1CCCCC
CCCCCCCCCC1CCCCC
1111111111111111
CCCC1CCCCCCCCCCC
CCCC1CCCCCCCCCCC
CCCC1CCCCCCCCCCC
1111111111111111
CC1CCCCCCCCC1CCC
CC1CCCCCCCCC1CCC
CC1CCCCCCCCC1CCC
1111111111111111
CCCCCCC1CCCCCCCC
CCCCCCC1CCCCCCCC
CCCCCCC1CCCCCCCC
1111111111111111`],
  [pushable, bitmap `
9999999999999999
9CC9CC9CC9CC9CC9
9CC9CC9CC9CC9CC9
9999999999999999
9CC9CC9CC9CC9CC9
9CC96C9CC9C69CC9
9996699999966999
9C666666666666C9
9C666666666666C9
9996699999966999
9CC96C9CC9C69CC9
9CC9CC9CC9CC9CC9
9999999999999999
9CC9CC9CC9CC9CC9
9CC9CC9CC9CC9CC9
9999999999999999`],
  [bg, bitmap `
7777777777777777
7777777777777777
7777777777777777
7777777777277777
7777777772227777
7777777722222777
7777777777777777
7777777777777777
7772277777777777
7722227777777777
7222222777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`]
)

setSolids([block, player , pushable])

let level = 0
const levels = [
  map`
........
........
........
........
p.d...f.
bbbbbbbb
bbbbbbbb`,
  map`
.........
.........
.........
.........
d.f..d..p
bbbbbbbbb
bbbbbbbbb`,
  map`
.............
.............
.............
.............
.p.d...d.f..d
bbbbbbbbbbbbb
bbbbbbbbbbbbb`,
  map`
.........
.........
.........
.........
p........
bb.ddbbbb
b.d....f.`,
  map`
...........
.p.........
bb.dbbbbbbb
bb....bbbbb
bbd.bbbbbbb
bbb........
bbb.bbbbbbf`,
  map`
ddd.........
..d.........
b.d.........
p.d.........
bbbbbbbbdbbb
bbbbbbb...fb
bbbbbbbb.bbb`
]

setMap(levels[level])
setBackground(bg)

setPushables({
  [player]: [pushable],
  [pushable]: [pushable]
})
const playback = playTune(song, Infinity)
onInput("d", () => {
  getFirst(player).x += 1
})
onInput("a", () => {
  getFirst(player).x -= 1
})
onInput("s", () => {
  getFirst(player).y += 1
}),
  onInput("w", () => {
  getFirst(player).y -= 1
}),
afterInput(() => {
  var playerflag = tilesWith(flag, player).length
  console.log(playerflag)
  if (playerflag != 0) {
    level = level + 1;

    const currentLevel = levels[level];

    // make sure the level exists and if so set the map
    // otherwise, we have finished the last level, there is no level
    // after the last level
    if (currentLevel !== undefined) {
      setMap(currentLevel);
    } else {
      addText("you win!", { y: 4, color: color`5` });
      playback.end()
      const playback2 = playTune(win)
    }
  }
})
